/**
 * Copyright (c) 2017-2022 Rushang Co.Ltd All Rights Reserved.
 */
package com.rshang.rsdp.util.beetl.func;

import com.rshang.rsdp.entity.TbUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.beetl.core.Context;
import org.beetl.core.Function;
import org.beetl.core.exception.BeetlException;

/**
 * 功能简介：自定义获取登陆成功的用户信息<br/>
 *
 * @author zhangjun
 * @create 2017年8月1日上午9:54:25
 */
public class SessionFunction implements Function {

    /* (non-Javadoc)
     * @see org.beetl.core.Function#call(java.lang.Object[], org.beetl.core.Context)
     */
    @Override
    public Object call(Object[] obj, Context context) {
        if (obj.length < 1 || obj.length > 1) {
            throw new BeetlException("调用Beetl方法获取用户信息，参数列表传递错误");
        }
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated() && currentUser.getPrincipal() != null) {
            TbUser user = (TbUser) currentUser.getPrincipal();
            String key = String.valueOf(obj[0]).trim().toLowerCase();
            switch (key) {
                case "userid":
                    return user.getUserId();
                case "username":
                    return user.getUserName();
                case "xm":
                    return user.getUserXm();
                case "phone":
                    return user.getUserPhone();
                case "gh":
                    return user.getUserGh();
                case "email":
                    return user.getUserEmail();
                default:
                    return "Key value not defined";
            }
        }
        return "";
    }

}
